package com.ruoyi.project.fit.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.fit.domain.FitDept;
import com.ruoyi.project.fit.service.FitDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fit/dept")
public class FitDeptController extends BaseController {

    @Autowired
    private FitDeptService FitDeptService;


    @Log(title = "科室数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('fit:dept:export')")
    @GetMapping("/export")
    public AjaxResult export(FitDept dept)
    {
        List<FitDept> list = FitDeptService.selectDeptList(dept);

        ExcelUtil<FitDept> util = new ExcelUtil<FitDept>(FitDept.class);
        return util.exportExcel(list, "科室数据");
    }

    /**
     * 删除科室
     */
    @PreAuthorize("@ss.hasPermi('fit:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds) {
        return toAjax(FitDeptService.deleteDeptByIds(deptIds));
    }

    /**
     * 修改科室
     */
    @PreAuthorize("@ss.hasPermi('fit:dept:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody FitDept dept) {

        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(FitDeptService.updateDept(dept));
    }
    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('fit:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody FitDept dept) {
        dept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(FitDeptService.insertDept(dept));
    }


    /**
     * 根据科室编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fit:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        return AjaxResult.success(FitDeptService.selectDeptById(deptId));
    }
    /**
     * 获取科室列表
     */
    @PreAuthorize("@ss.hasPermi('fit:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(FitDept dept) {
        startPage();
        List<FitDept> list = FitDeptService.selectDeptList(dept);
        return getDataTable(list);
    }

}
